package mg.daloji.antandroyd.application;

/*
 * Copyright (C) 2012 Johan Boris Iantila jiantila@gmail.com && http://www.facebook.com/johanboris.iantila
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import mg.daloji.antandroyd.R;
import mg.daloji.antandroyd.Radio;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


/**
 * Classe utilitaire de chargement des fichiers Xml
 * 
 * @author Johan Boris Iantila
 */
public class AppConfiguration {
   /** XML parsing constants */
   static final private String XML_TAG_PARAMETER = "parameter";
   static final private String XML_TAG_RADIO = "radio";
   static final private String XML_TAG_URL = "url";
   static final private String XML_TAG_TRI = "trigramme";
   static final private String XML_TAG_NOM = "nom";
   static final private String XML_TAG_LOGO = "logo";
   static final private String XML_ATTR_VALUE    = "value";
   static final private String XML_ATTR_NAME     = "name";

   /** The application context. */
   private  static Context mContext;

   /** Static (application hardcoded) configuration */
   static  LinkedList<Radio> radioConf = new LinkedList<Radio>();
   
   
   /**
    * Constructeur
    */
   public AppConfiguration(final Context appContext) {
       this.mContext = appContext;
       loadServiceConfiguration();
       
   }

   

   /**
    * Chargement  service configuration.
    */
   private void loadServiceConfiguration() {
	   
	   
	   Log.i("tag", "loadServiceConfiguration");
	   Radio radio=null;
	   String name=null;
             // Load static conf from XML
	   
      // final XmlResourceParser parser = this.mContext.getResources().getXml(R.xml.config);
       try {
    	   
    	   XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    	   factory.setNamespaceAware(true);
    	   XmlPullParser xpp = factory.newPullParser();
    	   
    	   Log.i("tag", "url :"+mContext.getString(R.string.url));
    	   URL input = new URL(mContext.getString(R.string.url));
    	   xpp.setInput(input.openStream(), null);
    	   
    	   
           while (XmlPullParser.END_DOCUMENT != xpp.getEventType()) {
        	   Log.i("RadioSifaka", "Parser :"+xpp.getName());
               if (XmlPullParser.START_TAG == xpp.getEventType() && xpp.getName().equals(XML_TAG_RADIO)) {
            	   if(radio !=null){
            		   Log.i("RadioSifaka", " finded new Radio Sifaka");
            		   radioConf.add(radio);
            		   radio=new Radio();
            	   }else{
            		   radio=new Radio();   
            	   }
               }
                   
               
               if (XmlPullParser.START_TAG == xpp.getEventType() && xpp.getName().equals(XML_TAG_PARAMETER)) {
            	    name = xpp.getAttributeValue(null, XML_ATTR_NAME);
            	    Log.i("RadioSifaka", " XML_TAG_URL : "+name);
            	   if(name!=null && name.equals(XML_TAG_URL)){
            		   final String value = xpp.getAttributeValue(null, XML_ATTR_VALUE);
            		   Log.i("RadioSifaka", " XML_TAG_URL : "+value);
            		   radio.setRadio(name, value);
            		   name=null;
            	   }
            	   name = xpp.getAttributeValue(null, XML_ATTR_NAME);
            	   if(name!=null && name.equals(XML_TAG_NOM)){
            		   final String value = xpp.getAttributeValue(null, XML_ATTR_VALUE);
            		   Log.i("RadioSifaka", " XML_TAG_TRI : "+value);
            		   radio.setRadio(name, value);
            		   name=null;
            	   }
            	   name = xpp.getAttributeValue(null, XML_ATTR_NAME);
            	   if(name!=null && name.equals(XML_TAG_LOGO)){
            		   final String value = xpp.getAttributeValue(null, XML_ATTR_VALUE);
            		   Log.i("RadioSifaka", " XML_TAG_NOM : "+value);
            		   radio.setRadio(name, value);
            		   name=null;
            	   }
            	    name = xpp.getAttributeValue(null, XML_ATTR_NAME);
            	    if(name!=null && name.equals(XML_TAG_TRI)){
            		   final String value = xpp.getAttributeValue(null, XML_ATTR_VALUE);
            		   Log.i("RadioSifaka", " XML_TAG_LOGO : "+value);
            		   radio.setRadio(name, value);
            		   name=null;
            	   }   
                   	
               }
               xpp.next();
           }
           radioConf.add(radio);
       } catch (XmlPullParserException e) {
           return;
       } catch (IOException e) {
           return;
       }
   }
   /**
    * retourne les paramètres de l'application
    * 
    * @return
    */
   public static LinkedList<Radio>  getParam(){

	   return radioConf;
   }
     
   
/**
 * Indicateur de debuggage 
 * 
 * @param context
 * @return 
 */
  public static boolean isDebug( Context context ) {
	    PackageManager pm = context.getPackageManager();
	    ApplicationInfo ai = new ApplicationInfo();
	    try {
	        ai = pm.getApplicationInfo( context.getPackageName(), 0 );
	    } catch( NameNotFoundException e ) {
	        ai = null;
	        return false;
	    }
	    if( (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) == ApplicationInfo.FLAG_DEBUGGABLE ) {
	        return true;
	    }
	    return false;
	}
}